package cn.mygweb.common.utils;

import java.io.File;
import java.util.Properties;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import cn.mygweb.admin.modules.tools.pojo.EmailServerConf;

/** 
* <p>Title: EmailUtil.java</p>
* <p>Description: 邮件工具类</p>  
* @author gxb 
* @date 2019年7月13日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Component
public class EmailUtil {
	
	JavaMailSenderImpl jms = new JavaMailSenderImpl();
	
	public EmailUtil() {
		
	}
	
	/**  
	* <p>Title: </p>
	* <p>Description: 初始化工具类，加载邮件配置信息</p>
	* @param emailServerConf
	*/
	public EmailUtil(EmailServerConf emailServerConf) {
		this.jms.setHost(emailServerConf.getHost());
		this.jms.setPort(emailServerConf.getPort());
		this.jms.setUsername(emailServerConf.getUsername());
		this.jms.setPassword(emailServerConf.getPassword());
		this.jms.setDefaultEncoding("UTF-8");
		Properties p = new Properties();
		p.setProperty("mail.smtp.auth", "true");
		this.jms.setJavaMailProperties(p);
	}
	
	/**
	 * <p>Title: sendSimpleMail</p>
	 * <p>Description: 发送html邮件或普通的文本邮件</p>
	 * @param senderName发件人名称
	 * @param to		收件邮箱
	 * @param subject	主题
	 * @param content	内容
	 * @throws Exception 
	 */
	public void sendHtmlMail(String senderName, String to, String subject, String content) throws Exception{
		MimeMessage mimeMessage = this.jms.createMimeMessage();
		//true 表示需要创建一个 multipart message
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom(this.jms.getUsername(), senderName);
		helper.setTo(to);
		helper.setSubject(subject);
		//true 表示支持html格式，不写或者false 则表示只是普通文本
		helper.setText(content, true);
		
		this.jms.send(mimeMessage);
	}
	
	/**
	 * <p>Title: sendInlineResourceMail</p>
	 * <p>Description: 发送带图片的邮件
	 * 	如果需要发送多张图片，可以改变传参方式，使用集合添加多个<img src='cid:rscId'>和 helper.addInline(rscId, res);</p>
	 * @param senderName发件人名称
	 * @param to		收件邮箱
	 * @param subject	主题
	 * @param content	内容
	 * @param rscPath	资源路径
	 * @param rscId		资源id
	 * @throws Exception 
	 */
	public void sendInlineResourceMail(String senderName, String to, String subject, String content,
			String rscPath, String rscId) throws Exception {
		MimeMessage mimeMessage = this.jms.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom(this.jms.getUsername(), senderName);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content, true);
		File file = new File(rscPath);
		FileSystemResource res = new FileSystemResource(file);
		helper.addInline(rscId, res);
		
		this.jms.send(mimeMessage);
	}
	
	/**
	 * <p>Title: sendAttachmentsMail</p>
	 * <p>Description: 发送带附件的邮件
	 * 	如果有多个附件，可以改变传参方式，使用集合多次调用helper.addAttachment(fileName, file);</p>
	 * @param senderName发件人名称
	 * @param to		收件邮箱
	 * @param subject	主题
	 * @param content	内容
	 * @param filePath	附件路径
	 * @throws Exception
	 */
	public void sendAttachmentsMail(String senderName, String to, String subject, 
			String content, String filePath) throws Exception {
		MimeMessage mimeMessage = this.jms.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom(this.jms.getUsername(), senderName);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content, true);
		FileSystemResource file = new FileSystemResource(new File(filePath));
		String fileName = file.getFilename();
		helper.addAttachment(fileName, file);
		
		this.jms.send(mimeMessage);
	}
	
	/**
	 * <p>Title: sendMail</p>
	 * <p>Description: 通用的发送邮件方法</p>
	 * @param senderName
	 * @param to
	 * @param subject
	 * @param content
	 * @param filePath
	 * @throws Exception
	 */
	public void sendMail(String senderName, String to, String subject, 
			String content, String filePath) throws Exception {
		MimeMessage mimeMessage = this.jms.createMimeMessage();
//		mimeMessage.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse("m13623642550@163.com"));//抄送
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
		helper.setFrom(this.jms.getUsername(), senderName);
		String[] toArr = to.split(",");
		helper.setTo(toArr);//多个收件人，以逗号分隔		
		helper.setSubject(subject);
		helper.setText(content, true);
		if(filePath != null) {
			String[] pathArr = filePath.split(",");
			for (String path : pathArr) {
				FileSystemResource file = new FileSystemResource(new File(path));
				String fileName = file.getFilename();
				helper.addAttachment(fileName, file);
			}
		}
		this.jms.send(mimeMessage);
	}
	
}
